﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Engine.NetClasses
{
    public partial class SetupOption : Form
    {
        private ServerOptions _options;
        public ServerOptions Options { set { _options = value; } }

        public SetupOption()
        {
            InitializeComponent();
            FormClosing += new FormClosingEventHandler(SetupOption_FormClosing);
        }

        void SetupOption_FormClosing(object sender, FormClosingEventArgs e)
        {
            Save();
            _options.FormClose();
        }

        private void SetupOption_Load(object sender, EventArgs e)
        {
            isRedirect.Checked = _options.IsRedirect;
            Addres.Items.AddRange(_options.AddressFamilyList);
            Addres.Text = _options.AddressFamilyCurrent;
            Port.Text = _options.Port.ToString();
            ServerListFile.Text = _options.ServerOptionFolder + _options.ServerListFile;
            OptionFile.Text = _options.ServerOptionFolder + _options.ServerOptionFile;
            protocol.Text = _options.Protocol.ToString();
            maxConnections.Text = _options.MaxConnection.ToString();
            buffersize.Text = _options.ReciveBufferSize.ToString();
            BaseLog.Text = _options.BaseLogDir;
            StateLog.Text =  _options.StateLogDir;
            ReciveLog.Text = _options.ReceiveLogDir;
            SendLog.Text =  _options.SendLogDir;
            Ping.Text = (_options.PingTime/1000).ToString();
            if (_options.ServerList != null)
            {
                for (int i = 0; i < _options.ServerList.Length; i++)
                {
                    object[] temp = new object[3];
                    temp[0] = _options.ServerList[i].ServerName;
                    temp[1] = _options.ServerList[i].IpServer;
                    temp[2] = _options.ServerList[i].PortServer;
                    ServerList.Rows.Add(temp);
                }

            }
        }

        private void isRedirect_CheckedChanged(object sender, EventArgs e)
        {
            if (isRedirect.Checked)
            {
                //ServerList.Enabled = true;
            }
            else
            {
               // ServerList.Enabled = true;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
           
            this.Close();
        }

        private void Save()
        {
             _options.IsRedirect=isRedirect.Checked;
             _options.AddressFamilyCurrent = Addres.Text;
            
            _options.Port = int.Parse(Port.Text);
            _options.Protocol = int.Parse(protocol.Text);
            _options.MaxConnection= int.Parse(maxConnections.Text);
            _options.ReciveBufferSize= int.Parse( buffersize.Text);
            _options.BaseLogDir = BaseLog.Text;            
            _options.StateLogDir = StateLog.Text.Replace(_options.BaseLogDir,"");
            _options.ReceiveLogDir= ReciveLog.Text.Replace(_options.BaseLogDir,"");
            _options.SendLogDir= SendLog.Text.Replace(_options.BaseLogDir,"");
            _options.PingTime = int.Parse(Ping.Text)*1000;
            if (isRedirect.Checked)
            {
                if (ServerList.Rows.Count>0)
                {
                    List<ServersList> tmp=new List<ServersList>();
                    foreach (DataGridViewRow item in ServerList.Rows)
                    {
                        if (item.Cells[0].Value != null && item.Cells[1].Value != null && item.Cells[2].Value != null)
                        {
                            tmp.Add(new ServersList((string)item.Cells[0].Value, (string)item.Cells[1].Value, (int)item.Cells[2].Value,false));
                        }
                        
                    }
                    if (tmp.Count>0)
                    {
                         _options.ServerList = tmp.ToArray();
                    }
                   
                }
            }
            else
            {
                _options.ServerList = null;
            }
            _options.ChangeOption = true;
           
        }


    }
}
